<?php

// 自义标签类
class tag {

    public $tag = array(
        'page' => array('block' => 0), //页码标签
    );

    public function _arclist($attr, $content) {
        $tui = isset($attr['tui']) ? "is_tui=" . $attr['tui'] : ""; //推荐属性
        $rows = isset($attr['rows']) ? $attr['rows'] : 10;
        $cid = isset($attr['cid']) ? "cat_id=" . $attr['cid'] : '';
        $str = '';
        $str.='<?php $db = M("arc");';
        $str.='$rows = $db->where("' . $tui . '")->where("' . $cid . '")->limit(' . $rows . ')->all();?>';
        $str.='<?php foreach($rows as $field):?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

    public function _channel($attr, $content) {
        $rows = isset($attr['rows']) ? $attr['rows'] : 10;
        $cid = isset($attr['cid']) ? $attr['cid'] : $_GET['cat_id'];
        $type = isset($attr['type']) ? $attr['type'] : "son";
        switch ($type) {
            case "son":
                $where = "cat_pid = " . $cid;
                break;
            case "top":
                $where = "cat_pid=0";
                break;
        }
        $str = '';
        $str.='<?php $db = M("category");';
        $str.='$rows = $db->where("' . $where . '")->limit(' . $rows . ')->all();';
        $str.='foreach($rows as $field):?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

    function _channels($attr, $content) {
        $str = '';
        $where = $attr['cid']; # $field['cat_id']
        $str.='<?php $cate_s = M("category");';
        $str.='$result = $cate_s->where("cat_pid=".' . $where . ')->all();';
        $str.='if($result):';
        $str.='foreach($result as $field):?>';
        $str.=$content;
        $str.='<?php endforeach;';
        $str.='endif;?>';
        return $str;
    }

    function _pagelist($attr, $content) {
        $rows = isset($attr['rows']) ? $attr['rows'] : 10;
        $cat_id = isset($_GET['cat_id']) ? $_GET['cat_id'] : false;
        if ($cat_id === false)
            return 'pagelist标签只能在列表页中使用';
        $str = '';
        $str.='<?php $db = M("arc");';
        $str.='$total = $db->where("cat_id=' . $cat_id . '")->count();';
        $str.='$page = new page($total,' . $rows . ');';
        $str.='$rows = $db->limit($page->limit())->all();';
        $str.='foreach($rows as $field):';
        $str.='$field["arcurl"] = \'<a href="__CONTROL__/arc/arc_id/\'.$field["arc_id"].\'">\'.$field["title"].\'</a>\';?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

    function _page($attr, $content) {
        $style = isset($attr['style']) ? $attr['style'] : 2;
        return '<?php echo $page->show(' . $style . ');?>';
    }

}

?>